<template>
  <view>
    <!-- 已登录状态显示的内容 -->
    <view v-if="isLoginFlag">
      <view style="background-color:#f8f8f8;">
    <view class="user-profile-container">
      <view class="header">
        <view class="avatar-container">
          <image class="avatar" :src="avatar" mode="aspectFill"></image> 
        </view>
        <view class="user-info">
          <view style="margin-top:-10px;">
            <text class="username">{{loginUserInfo.username}}</text>
          </view>
        </view>
      </view>
    </view>
    <view style="background-color:#f8f8f8;height:600px;">
      <!-- 功能列表部分 -->
      <view class="function-list">
        <view class="function-item" @click="navigateToAddress">
          <image class="icon-image" src="/static/icon/地址.png" />
          <text class="function-text">我的地址</text>
        </view>
        <view class="function-item" @click="navigateToFollow">
          <image class="icon-image" src="/static/icon/我的关注.png" />
          <text class="function-text">我的关注</text>
        </view>
        <view class="function-item" @click="navigateToOrder">
          <image class="icon-image" src="/static/icon/订单.png" />
          <text class="function-text">我的订单</text>
        </view>
      </view>
      <!-- 退出登录按钮部分 -->
      <button class="logout-btn" @click="logout">退出登录</button>
    </view>
  </view>
    </view>
    <!-- 未登录状态显示的内容 -->
    <view v-if="!isLoginFlag">
          <view class="register-link" @click="goTologin">还未登录，去登录</view>
    </view>
  </view>
</template>

<script>
	import  config  from '@/config'
export default {
  data() {
    return {
      isLoginFlag: false, 
	  avatar: '/static/imgs/assets/header.png',// 登录状态标志，初始为未登录
      loginUserInfo: {
        name: '',
      }
    };
  },
   onShow() {
    try {
      const value = uni.getStorageSync('token');
      const loginUserInfoJSON = uni.getStorageSync('loginUserInfo');
      if (value) {
        this.loginUserInfo = JSON.parse(loginUserInfoJSON);
        this.isLoginFlag = true;
      } else {
        this.isLoginFlag = false;
      }
    } catch (e) {
      console.error(e);
    }
	console.log(this.loginUserInfo)
  },
  methods: {
	navigateToAddress() {
      uni.navigateTo({
        url: '/pages/address/address' 
      });
    },
    navigateToFollow() {
      uni.navigateTo({
        url: '/pages/many/index' 
      });
    },
    navigateToOrder() {
      uni.switchTab({
        url: '/pages/order/index' 
      });
    },
    goTologin() {
      uni.navigateTo({
        url: '/pages/login/index'
      });
    },
	logout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
			uni.clearStorageSync();
            uni.showToast({
			title: '退出成功',
			icon: 'success',
			isLoginFlag: false
		});
		uni.navigateTo({
		  url: '/pages/login/index' 
		});
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.user-profile-container {
  background-image: linear-gradient(239deg, #3a6ff3, #50c7fb);
  padding: 20px;
  margin-bottom: 20px; 
  height: 180px;
}

.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: white;
  border-radius: 10px;
  padding: 20px;
  height: 90px; 
  margin-top: 55px;
}
.register-link {
    font-size: 16px;
    color: blue;
    text-decoration: underline;
    text-align: center;
    margin-top: 20px;
    cursor: pointer;
}
.avatar-container {
  margin-bottom: 15px;
}

.avatar {
  width: 90px;
  height: 90px;
  border-radius: 50%;
  margin-top: -60px;
}

.user-info {
  text-align: center;
}

.username {
  font-size: 23px;
}

.user-id {
  color: #999;
  font-size: 14px;
}

/* 功能列表样式 */
.function-list {
  background-color: white;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 20px; 
  margin-top: 20px;
  margin-left: 23px;
  width: 90%;
  border: 1px solid #f1f1f1;
}

.function-item {
  display: flex;
  align-items: center;
  padding: 23px 20px;
  border-bottom: 1px solid #e0e0e0;
  cursor: pointer;
}

.function-item:last-child {
  border-bottom: none;
}

.icon {
  width: 18px;
  height: 18px;
}

.icon-image {
  margin-right: 15px;
  width: 24px;
  height: 24px;
  border-radius: 35%;
  text-align: center;
  font-weight: 700;
  color: #fff;
}

.function-text {
  flex: 1;
  font-size: 16px;
}

.arrow {
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
  color: #999;
}

/* 退出登录按钮样式 */
.logout-btn {
  background-color: #007aff;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 5px;
  width: 90%;
  height: 50px;
  font-size: 16px;
  cursor: pointer;
}
</style>